package com.example.testrecorder.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.testrecorder.AddRecordActivity;

/**
 * @author Yingyong Lao
 * 创建时间 2021/6/13 8:26
 * @version 1.0
 */
public class PermissionUtil {
    public static final int WRITE_AND_READ=1;//读写权限的请求码
    public static final int RWC=2;//读写权限、相机权限的请求码

    /**
     * 请求读写权限
     * @param activity
     */
    public static void reqWriteAndReadPermission(Activity activity, PermittedCallBack permittedCallBack){
        if(Build.VERSION.SDK_INT>=23){//6.0以上版本，加入动态权限申请
            int i = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);//写权限包括了读权限
            if(i!=PackageManager.PERMISSION_GRANTED){//没有授予读写权限
                //请求读写权限
                ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},WRITE_AND_READ);
            }else{//授予了读写权限
                permittedCallBack.permitted();
            }
        }else {//6.0以下版本，安装了应用就代表已经有读写权限了
            permittedCallBack.permitted();
        }

    }

    /**
     * 请求读写权限
     * @param activity
     */
    public static void reqWriteAndReadPermission(Activity activity, PermittedCallBack permittedCallBack,int requestCode){
        if(Build.VERSION.SDK_INT>=23){//6.0以上版本，加入动态权限申请
            int i = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);//写权限包括了读权限
            if(i!=PackageManager.PERMISSION_GRANTED){//没有授予读写权限
                //请求读写权限
                ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode);
            }else{//授予了读写权限
                permittedCallBack.permitted();
            }
        }else {//6.0以下版本，安装了应用就代表已经有读写权限了
            permittedCallBack.permitted();
        }

    }

    /**
     * 请求读写权限、相机权限
     * @param activity
     */
    public static void reqRWCPermission(Activity activity, PermittedCallBack permittedCallBack){
        if(Build.VERSION.SDK_INT>=23){//6.0以上版本，加入动态权限申请
            int i = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);//写权限包括了读权限
            int i2 = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);//写权限包括了读权限
            if(i!=PackageManager.PERMISSION_GRANTED||i2!=PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA},RWC);
            }else{
                permittedCallBack.permitted();
            }
        }else {//6.0以下版本，安装了应用就代表已经有读写权限了
            permittedCallBack.permitted();
        }

    }

    /**
     * 已经授予了权限的回调
     */
    public interface PermittedCallBack{
        public void permitted();
    }
}
